package cn.pconline.autoclub.service.bbssync;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.DynaInfo;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;
import cn.pconline.officialclub.service.ODynaService;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月4日 上午10:22:38
 */
public class SyncRecoverTopicService extends BbsSyncPetrolService {
	@Autowired
	private DynaService dynaService;
	@Autowired
	private PetrolService petrolService;
        @Autowired
	private ODynaService oDynaService;
	@Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
	    int total = 0;
	    long topicId = getTopicId(msg);
        Date createAt = getTopicCreateAt(msg);
        DynaInfo dynaInfo = dynaService.findDynaInfoByTopicId(topicId, createAt);
        if(dynaInfo != null) {
            ClubIntegral topicClubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_TOPIC); // 油值表
            if(topicClubIntegral != null) {
                String reason = topicClubIntegral.getName() + "(topicId-" + dynaInfo.getTopicId() + ")";
                petrolService.addOnlyPersonWithinLimit(clubMember, topicClubIntegral.getIntegralId(), topicClubIntegral.getPetrol(), reason, true);
                total += topicClubIntegral.getPetrol();
            }
            if (dynaInfo.getPick() > 0) {
                ClubIntegral pickClubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK);
                //如果帖子恢复为精华帖，加精华分数
                if (pickClubIntegral != null) {
                    String reason = pickClubIntegral.getName() + "(topicId-" + dynaInfo.getTopicId() + ")";
                    petrolService.addOnlyPersonWithinLimit(clubMember, (int) pickClubIntegral.getIntegralId(), pickClubIntegral.getPetrol(), reason, false);
                    total += pickClubIntegral.getPetrol();
                }
            }
            if (dynaInfo.getRecommend() > 0) {
                ClubIntegral recommandClubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_RECOMMEND);;
                //如果帖子恢复为推荐帖，加推荐分数
                if (recommandClubIntegral != null) {
                    String reason = recommandClubIntegral.getName() + "(topicId-" + dynaInfo.getTopicId() + ")";
                    petrolService.addOnlyPersonWithinLimit(clubMember,(int)recommandClubIntegral.getIntegralId(), recommandClubIntegral.getPetrol(), reason, false);
                    total += recommandClubIntegral.getPetrol();
                }
            }
        } else {
            log.info("not found recover topic[{}]", topicId);
        }
        return total;
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        int total = 0;
        long topicId = getTopicId(msg);
        Date createAt = getTopicCreateAt(msg);
        DynaInfo dynaInfo = dynaService.findDynaInfoByTopicId(topicId, createAt);
        if(dynaInfo != null) {
            ClubIntegral topicClubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_TOPIC); // 油值表
            if(topicClubIntegral != null) {
                String reason = topicClubIntegral.getName() + "(topicId-" + dynaInfo.getTopicId() + ")";
                petrolService.addToClubWithinLimit(club.getClubId(), topicClubIntegral.getIntegralId(), topicClubIntegral.getPetrol(), reason, true);
                total += topicClubIntegral.getPetrol();
            }
            //如果帖子恢复为精华帖，加精华分数
            if (dynaInfo.getPick() > 0) {
                ClubIntegral pickClubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK);
                if (pickClubIntegral != null) {
                    String reason = pickClubIntegral.getName() + "(topicId-" + dynaInfo.getTopicId() + ")";
                    petrolService.addToClubWithinLimit(club.getClubId(), (int) pickClubIntegral.getIntegralId(), pickClubIntegral.getPetrol(), reason, false);
                    total += pickClubIntegral.getPetrol();
                }
            }
            //如果帖子恢复为推荐帖，加推荐分数
            if (dynaInfo.getRecommend() > 0) {
                ClubIntegral recommandClubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_RECOMMEND);;
                if (recommandClubIntegral != null) {
                    String reason = recommandClubIntegral.getName() + "(topicId-" + dynaInfo.getTopicId() + ")";
                    petrolService.addToClubWithinLimit(club.getClubId(),(int)recommandClubIntegral.getIntegralId(), recommandClubIntegral.getPetrol(), reason, false);
                    total += recommandClubIntegral.getPetrol();
                }
            }
        } else {
            log.info("not found recover topic[{}]", topicId);
        }
        return total;
    }
    
    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("topicAuthorId");
    }
    
	@Override
	public void process(JSONObject msg, ClubMember clubMember) {
	    
		//验证用户
	    long userId = getUserId(msg);
	    if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }

        long topicId = getTopicId(msg);
        Date createAt = getTopicCreateAt(msg);
        DynaInfo dynaInfo = dynaService.findDynaInfoByTopicId(topicId, createAt);
        //恢复帖子
        if (dynaInfo != null) {
            dynaService.rollbackClubDyna(dynaInfo.getClubId(), dynaInfo.getDynaInfoId());//修改车友会动态分表
            oDynaService.rollbackClubDyna(dynaInfo.getClubId(), dynaInfo.getDynaInfoId());//修改官方车友会动态分表
            dynaService.rollbackUserDyna(dynaInfo.getCreateBy(), dynaInfo.getDynaInfoId());//修改用户动态分表
        }
	}
    
    private long getTopicId(JSONObject msg) {
        return msg.getLongValue("topicId");
    }
    
    private Date getTopicCreateAt(JSONObject msg) {
        return msg.getDate("topicCreateAt");
    }
}
